//
//  WLUserHeadVController.m
//  WanLian
//
//  Created by muzhuang on 2018/11/8.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLUserHeadVController.h"

@interface WLUserHeadVController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *ivPhoto;
@property (nonatomic, strong) UIScrollView *scrollPhoto;

@end

@implementation WLUserHeadVController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的头像";
    
    _scrollPhoto = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN_WL, HEIGHT_SCREEN_WL-WL_NavTopHeight)];//scrollView作为背景
    _scrollPhoto.delegate = self;
    _scrollPhoto.userInteractionEnabled = YES;
    _scrollPhoto.showsHorizontalScrollIndicator = YES;//是否显示侧边的滚动栏
    _scrollPhoto.showsVerticalScrollIndicator = NO;
    _scrollPhoto.scrollsToTop = NO;
    _scrollPhoto.scrollEnabled = YES;
    //    _scrollPhoto.backgroundColor = [UIColor blackColor];
    
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgViewGestureR:)];
    [tapBg setNumberOfTapsRequired:2];
    [_scrollPhoto addGestureRecognizer:tapBg];
    
    _ivPhoto = [[UIImageView alloc] initWithFrame:_scrollPhoto.bounds];
    //    _ivPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _ivPhoto.contentMode = UIViewContentModeScaleAspectFit;
    _ivPhoto.image = _headImage;
//    [_ivPhoto sd_setImageWithURL:[NSURL URLWithString:_photoModel.filepath] placeholderImage:nil];
    [_scrollPhoto addSubview:_ivPhoto];
    [self.view addSubview:_scrollPhoto];
    
    //    WLWkSelf
    //    [_scrollPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.edges.equalTo(weakSelf.view);
    //    }];
    
    //最大放大比例
    _scrollPhoto.maximumZoomScale = 2;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _ivPhoto;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (_scrollPhoto.bounds.size.width > _scrollPhoto.contentSize.width)?(_scrollPhoto.bounds.size.width - _scrollPhoto.contentSize.width) *0.5 : 0.0;
    CGFloat offsetY = (_scrollPhoto.bounds.size.height > _scrollPhoto.contentSize.height)?
    (_scrollPhoto.bounds.size.height - _scrollPhoto.contentSize.height) *0.5 : 0.0;
    _ivPhoto.center =CGPointMake(_scrollPhoto.contentSize.width *0.5 + offsetX,_scrollPhoto.contentSize.height *0.5 + offsetY);
}

- (void)tapBgViewGestureR:(UITapGestureRecognizer *)tapGR
{
    CGFloat newScale;
    if(_scrollPhoto.zoomScale>1){
        newScale=1.0;
    }
    else{
        newScale=2;
    }
    //    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[sender locationInView:sender.view]];
    CGRect zoomRect;
    zoomRect.size.height =_scrollPhoto.frame.size.height / newScale;
    zoomRect.size.width  =_scrollPhoto.frame.size.width  / newScale;
    zoomRect.origin.x = _ivPhoto.center.x - (zoomRect.size.width  /2);
    zoomRect.origin.y = _ivPhoto.center.y - (zoomRect.size.height /2);
    
    [_scrollPhoto zoomToRect:zoomRect animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
